Ext.define('Jwawa.framework.JwawaConnection', {
    extend: 'Ext.data.Connection',
    singleton: true,
    autoAbort : false,
//    extraParams: {isajax: true},
    parseStatus: function(request) {
        // see: https://prototype.lighthouseapp.com/projects/8886/tickets/129-ie-mangles-http-response-status-code-204-to-1223
        var me = this,
            response;

        var status = request.xhr.status;
        status = status == 1223 ? 204 : status;

        try {
            if ((status >= 200 && status < 300) || status == 304) {
                // SERVER_CATCHED_ERROR or success
                response = me.createResponse(request);
                response.responseJson["status"] = status;
            } else if (status == 12002
                    || status == 12029
                    || status == 12030
                    || status == 12031
                    || status == 12152
                    || status == 13030
                    || request.aborted
                    || request.timedout) {
                // CLIENT_SEND_ERROR
                response = me.createException(request);
                response.responseJson["status"] = status;
                response.responseJson["success"] = false;
                response.responseJson["message"] = "数据发送异常! status:" + status
                    + ", abort:" + request.aborted
                    + ", timedout:" + request.timedout;
            } else {
                // SERVER_UNCATCHED_ERROR
                response = me.createResponse(request);
                response.responseJson["status"] = status;
                response.responseJson["success"] = false;
                response.responseJson["message"] = "服务器异常! status:" + status + "\n";
                response.responseJson["message"] += response.responseText;
            }
        } catch (e) {
            response = {responseJson : {
                    success: false,
                    message: e.message
                }
            };
//            Ext.namespace("response.responseJson");
//            responseJson["success"] = false;
//            responseJson["message"] = e.message;
        }

        return response;
    },
    onComplete : function(request) {
        var me = this,
            options = request.options,
            success,
            response;

        response = me.parseStatus(request);
        success = response.responseJson["success"];

        if (success) {
            me.fireEvent('requestcomplete', me, response.responseJson, options);
            Ext.callback(options.success, options.scope, [response.responseJson, options]);
        } else {
            me.fireEvent('requestexception', me, response, options);
            Ext.callback(options.failure, options.scope, [response.responseJson, options]);
        }
        Ext.callback(options.callback, options.scope, [options, success, response.responseJson]);
        delete me.requests[request.id];
        return response;
    },

    createResponse : function(request) {
        var xhr = request.xhr,
            headers = {},
            lines = xhr.getAllResponseHeaders().replace(/\r\n/g, '\n').split('\n'),
            count = lines.length,
            line, index, key, value, response;

        while (count--) {
            line = lines[count];
            index = line.indexOf(':');
            if (index >= 0) {
                key = line.substr(0, index).toLowerCase();
                if (line.charAt(index + 1) == ' ') {
                    ++index;
                }
                headers[key] = line.substr(index + 1);
            }
        }

        request.xhr = null;
        delete request.xhr;

        response = {
            request: request,
            requestId : request.id,
            status : xhr.status,
            statusText : xhr.statusText,
            getResponseHeader : function(header) {
                return headers[header.toLowerCase()];
            },
            getAllResponseHeaders : function() {
                return headers;
            }
//            responseText : xhr.responseText,
//            responseXML : xhr.responseXML,
//            responseJson: Ext.decode(xhr.responseText)
        };

        try {
            response['responseJson'] = Ext.decode(xhr.responseText);
        } catch(e) {
            response['responseText'] = xhr.responseText;
            response['responseXML'] = xhr.responseXML;
            response['responseJson'] = {};
        }

        // If we don't explicitly tear down the xhr reference, IE6/IE7 will hold this in the closure of the
        // functions created with getResponseHeader/getAllResponseHeaders
        xhr = null;
        return response;
    },

    createException : function(request) {
        return {
            request : request,
            requestId : request.id,
            status : request.aborted ? -1 : 0,
            statusText : request.aborted ? 'transaction aborted' : 'communication failure',
            aborted: request.aborted,
            timedout: request.timedout,
            responseJson: {}
        };
    }
});
